﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ProgramCat
{
    /// <summary>
    /// com帮助类 主要用于动态反射创建
    /// </summary>
    internal static class ComHelperClass
    {
        public const string s_IID_IComClass = "C66C0654-49AE-4f2e-8EDA-BD01C8259C20";
        public const string s_CLSID_ComClass = "12D783BB-33BF-4973-B38B-2A8F0BA926E4";
        public static readonly Guid IID_ICom = new Guid(s_IID_IComClass);
        public static readonly Guid CLSID_ComClass = new Guid(s_CLSID_ComClass);

        public const string s_IID_IClassFactory = "00000001-0000-0000-C000-000000000046";
        public static readonly Guid IID_IClassFactory = new Guid("00000001-0000-0000-C000-000000000046");
        public static readonly Guid IID_IUnknown = new Guid("00000000-0000-0000-C000-000000000046");

        [DllImport("ole32.dll")]
        public static extern int CoRegisterClassObject(
            [MarshalAs(UnmanagedType.LPStruct)] Guid rclsid,
            [MarshalAs(UnmanagedType.IUnknown)] object pUnk,
            uint dwClsContext,
            uint flags,
            out uint lpdwRegister);

        [DllImport("ole32.dll")]
        public static extern int CoRevokeClassObject(uint dwRegister);

        [DllImport("ole32.dll")]
        public static extern int CoInitializeSecurity(
         IntPtr securityDescriptor,
         Int32 cAuth,
         IntPtr asAuthSvc,
         IntPtr reserved,
         UInt32 AuthLevel,
         UInt32 ImpLevel,
         IntPtr pAuthList,
         UInt32 Capabilities,
         IntPtr reserved3);

        public const int RPC_C_AUTHN_LEVEL_PKT_PRIVACY = 6; // Encrypted DCOM communication
        public const int RPC_C_IMP_LEVEL_IDENTIFY = 2; // No impersonation really required
        public const int CLSCTX_LOCAL_SERVER = 4;
        public const int REGCLS_MULTIPLEUSE = 1;
        public const int EOAC_DISABLE_AAA = 0x1000; // Disable Activate-as-activator
        public const int EOAC_NO_CUSTOM_MARSHAL = 0x2000; // Disable custom marshalling
        public const int EOAC_SECURE_REFS = 0x2;   // Enable secure DCOM references
        public const int CLASS_E_NOAGGREGATION = unchecked((int)0x80040110);
        public const int E_NOINTERFACE = unchecked((int)0x80004002);
    }
    // 类厂
    [
     ComImport,
     InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
     Guid(ComHelperClass.s_IID_IClassFactory)
    ]
    internal interface IClassFactory
    {
        [PreserveSig]
        int CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject);
        [PreserveSig]
        int LockServer(bool fLock);
    }
    internal class ComClassFactory : IClassFactory
    {
        #region IClassFactory Members

        public int CreateInstance(IntPtr pUnkOuter, ref Guid riid, out IntPtr ppvObject)
        {
            ppvObject = IntPtr.Zero;
            if (pUnkOuter != IntPtr.Zero)
                Marshal.ThrowExceptionForHR(ComHelperClass.CLASS_E_NOAGGREGATION);
            if (riid == ComHelperClass.IID_ICom ||
                 riid == ComHelperClass.IID_IUnknown)
            {
                ppvObject = Marshal.GetComInterfaceForObject(
                    new AppCom(), typeof(IAppCom));
            }
            else
                Marshal.ThrowExceptionForHR(ComHelperClass.E_NOINTERFACE);
            return 0; // S_OK
        }
        public int LockServer(bool fLock)
        {
            return 0; // S_OK
        }
        #endregion
    }












    
}
